<?php

class MainModule extends CWebModule
{

	public $defaultController = 'main';
	public $layout = '//layouts/main';
	public $userRegion = [ ];

	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application
		// import the module-level models and components
//		$this->setImport(array(
//			'Main.models.*',
//			'Main.components.*',
//		));
		Yii::app()->setComponents( array(
			'user' => array(
				'allowAutoLogin' => true,
				'class' => 'CWebUser',
				'loginUrl' => '/login/',
			)
				)
		);
		Yii::app()->clientScript->registerCoreScript( 'jquery' );
		Yii::app()->clientScript->registerCoreScript( 'maskedinput' );
		if( !isset( Yii::app()->session[ 'userRegion' ] ) )
		{
			$defaultRegion = mTools::getDefaultRegion();
			//$data = mTools::getRegion();

			$this->userRegion[ 'country' ] = 'Не определена';
			$this->userRegion[ 'region' ] = 'Не определен';
			$this->userRegion[ 'city' ] =  $defaultRegion['regionTitle'];
			$this->userRegion['id'] = $defaultRegion['regionId'];
			$this->userRegion['isNewVisitor'] = true;
			
//			Yii::app()->session[ 'userRegion' ] = $this->userRegion;
			
			Yii::app()->clientScript->registerScript( "setRegion", "mobi03.setUData( { 'userRegion': {'title':'" . $this->userRegion[ 'city' ] . "', 'id':".$this->userRegion['id']."} } ); ", CClientScript::POS_READY );
		}
		else
		{
			$this->userRegion = Yii::app()->session[ 'userRegion' ];
		}
	}

	public function beforeControllerAction( $controller, $action )
	{
		if( parent::beforeControllerAction( $controller, $action ) )
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
			return true;
		}
		else
			return false;
	}

}
